﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
namespace ProductManagement.Controls
{
    public delegate void SendMessageToThePageHandler(string messageToThePage);
    public partial class ProductList : System.Web.UI.UserControl
    {
        public event SendMessageToThePageHandler sendMessageToThePage;
        public DataTable dtProductList = new DataTable();
        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    if (!Page.IsPostBack)
        //    {
        //        loadData();
        //    }
        //}

        //public void LoadData()
        //{
        //    XmlDocument vidDocument = new XmlDocument();
        //    vidDocument.Load(Server.MapPath("~/ProductList.xml"));
        //    XmlNodeList products = vidDocument.SelectNodes("/products/product");

        //}

        //public void loadData()
        //{
        //    DataSet ds = new DataSet("Root");


        //    ds.ReadXml(Server.MapPath("~/ProductList.xml"));


        //    grdProductList.DataSource = ds.Tables["Product"];
        //    grdProductList.DataBind();
        //    lblCount.Text = grdProductList.Rows.Count.ToString();
        //    dtProductList = ds.Tables["Product"];
        //    //if (sendMessageToThePage != null)
        //    //{

        //    //    sendMessageToThePage("10");

        //    //}
        //}

        //private string FuncSort(SortDirection sortDirection)
        //{
        //    string newSortDirection = String.Empty;
        //    switch (sortDirection)
        //    {
        //        case SortDirection.Ascending:
        //            newSortDirection = "ASC";
        //            break;
        //        case SortDirection.Descending:
        //            newSortDirection = "DESC";
        //            break;
        //    }
        //    return newSortDirection;
        //}

        //protected void grdProductList_Sorting(object sender, GridViewSortEventArgs e)
        //{

        //    DataTable dtTable = grdProductList.DataSource as DataTable;
        //    if (dtTable != null)
        //    {
        //        DataView dtView = new DataView(dtTable);
        //        dtView.Sort = e.SortExpression + " " + FuncSort(e.SortDirection);
        //        grdProductList.DataSource = dtView;
        //        grdProductList.DataBind();
        //    }

        //}

        //public event SelectedProductChanged;
        protected int _selectedid;

        public int SelectedProductId
        {
            get
            {
                return _selectedid;
            }
            set
            {
                _selectedid = value;
                txtSelectedId.Value = grdProductList.SelectedDataKey.Value.ToString();
            }
        }



        protected void grdProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (grdProductList.SelectedDataKey.Value != null)
            {
                _selectedid = int.Parse(grdProductList.SelectedDataKey.Value.ToString());
                if (sendMessageToThePage != null)
                {

                    sendMessageToThePage(_selectedid.ToString());

                }
                //event SelectedProductChanged;
            }
        }

    }
}